package BookManagement.dao;

import BookManagement.domain.Borrow;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

@Repository
public interface BorrowDao {

    @Select("select * from borrow where bookISBN=#{bookISBN} and username=#{userName}")
    public Borrow searchBorrowingBook(HashMap<String,Object> map);

    @Insert("insert into borrow values(#{userName},#{bookISBN},#{bookName},#{length},#{timeStart})")
    public int borrowBook(Borrow borrow);

    @Delete("delete from borrow where bookISBN=#{bookISBN} and username=#{userName}")
    public int backBook(HashMap<String,Object> map);

    @Update("update borrow set length=${length} where bookISBN=#{bookISBN} and username=#{username}")
    public int renewBook(HashMap<String,Object> map);

    @Select("<script>"+"select * from borrow where username=#{username}"+
            "<if test='start!=null and size!=null'>"+"limit #{start},#{size}"+"</if>"
            +"</script>"
    )
    public List<Borrow> searchAllBorrowingBookWithUsername(HashMap<String,Object> map);

    @Select("<script>"+"select * from borrow where bookISBN=#{bookISBN}"+
            "<if test='start!=null and size!=null'>"+"limit #{start},#{size}"+"</if>"
            +"</script>"
    )
    public List<Borrow> searchAllBorrowingBookWithBookISBN(HashMap<String,Object> map);

    @Select("<script>"+"select * from borrow"+
            "<if test='start!=null and size!=null'>"+"limit #{start},#{size}"+"</if>"
            +"</script>"
    )
    public List<Borrow> searchAllBorrowingBook(HashMap<String,Object> map);

    @Select("select count(*) from borrow")
    public int getTotalBorrowing();

    @Select("select count(*) from borrow where username=#{username}")
    public int findBorrowCountByUser(String username);

    @Select("select count(*) from borrow where bookISBN=#{bookISBN}")
    public int findBorrowCountByBook(String bookISBN);
}
